package cn.com.thingo.hyintelligent.ui.activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.EzDeviceAuthorizationContract;
import cn.com.thingo.hyintelligent.model.bean.BaseBean;
import cn.com.thingo.hyintelligent.model.bean.EzDeviceAuthorizationListBean;
import cn.com.thingo.hyintelligent.model.bean.PermissionJsonBean;
import cn.com.thingo.hyintelligent.model.db.UserInfo;
import cn.com.thingo.hyintelligent.presenter.EzDeviceAuthorizationPresenter;
import cn.com.thingo.hyintelligent.ui.adapter.EzDeviceAuthorizationAdapter;
import cn.com.thingo.hyintelligent.ui.adapter.decoration.RecycleViewDivider;
import cn.com.thingo.hyintelligent.util.GsonUtil;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import cn.com.thingo.hyintelligent.util.WaitDialogUtils;
import cn.com.thingo.hyintelligent.widget.WaitDialog;

public class EzDeviceAuthorizationActivity extends BaseActivity<EzDeviceAuthorizationPresenter> implements EzDeviceAuthorizationContract.View {
    @BindView(R.id.auth_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.toolbar_left)
    ImageView ivLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.toolbar_right)
    ImageView ivRight;
    private UserInfo mUserInfo;
    EzDeviceAuthorizationAdapter authorizationAdapter;
    private EzDeviceAuthorizationListBean mEzDeviceAuthorizationListBean;
    private BroadcastReceiver mReceiver;
    public final static String ACTION_REFRESH = "refresh";
    private WaitDialog mWaitDialog;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_ezdevice_authorization;
    }

    @Override
    protected void initEventAndData() throws Exception {
        mUserInfo = UserInfo.getInstance();
        ivLeft.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.my_auth);
        ivRight.setImageResource(R.mipmap.add);
        ivRight.setVisibility(View.VISIBLE);
        initView();
    }

    private void initView() {
        authorizationAdapter = new EzDeviceAuthorizationAdapter();
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(authorizationAdapter);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        mPresenter.getDevicePermissionList(mUserInfo.getAccessToken(), mUserInfo.getUserName(), null);
        authorizationAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(final BaseQuickAdapter adapter, View view, final int position) {
                switch (view.getId()) {
                    case R.id.item_device_delete:
                        new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT).setTitle("取消授权").setNegativeButton("取消", null).setNeutralButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                mWaitDialog = WaitDialogUtils.showWaitDialog(mContext);
                                mEzDeviceAuthorizationListBean = (EzDeviceAuthorizationListBean) adapter.getItem(position);
                                mPresenter.deleteEzDevicePermission(mUserInfo.getAccessToken(), mUserInfo.getUserName(), mEzDeviceAuthorizationListBean.getTo_tel(), mEzDeviceAuthorizationListBean.getDev_sn());

                            }
                        }).show();


                }
            }
        });
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                assert action != null;
                switch (action) {
                    case ACTION_REFRESH:
                        mPresenter.getDevicePermissionList(mUserInfo.getAccessToken(), mUserInfo.getUserName(), null);
                        break;
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_REFRESH);
        LocalBroadcastManager.getInstance(mContext).registerReceiver(mReceiver, filter);
    }

    @OnClick({R.id.toolbar_left, R.id.toolbar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
            case R.id.toolbar_right:
                startActivity(new Intent(EzDeviceAuthorizationActivity.this, EzDeviceManagerActivity.class));
                break;
        }
    }


    @Override
    public void viewDeleteDevicePermission(BaseBean bean) {
        if (mWaitDialog != null) {
            WaitDialogUtils.dismissWaitDialog(mWaitDialog);
        }
        if (bean.getStatus() == 1) {
            mPresenter.getDevicePermissionList(mUserInfo.getAccessToken(), mUserInfo.getUserName(), null);
        }
        ToastUtil.show(bean.getMsg());
    }

    @Override
    public void viewGetDevicePermissionList(BaseBean<List<EzDeviceAuthorizationListBean>> bean) {
        if (bean.getStatus() == 1) {
            authorizationAdapter.setNewData(null);
            List<EzDeviceAuthorizationListBean> data = bean.getData();
            for (EzDeviceAuthorizationListBean listBean :
                    data) {
                if (!listBean.getTo_tel().equals(mUserInfo.getUserName())) {
                    authorizationAdapter.addData(listBean);
                }
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            unregisterReceiver(mReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
